package com.cneport.das.common.utils;

import cn.hutool.crypto.digest.MD5;
import com.cneport.das.common.model.param.ClientRequest;
import com.cneport.das.common.model.param.ClientResponse;
import com.cneport.das.common.model.trans.ClientGetRequest;
import com.cneport.das.common.model.trans.ClientPutRequest;


/**
 * @Desc 软加签
 * @Ver 1.0
 * @Author GavinZhang
 * @Company 东方物通科技有限（北京）公司
 * @Date 2021-12-14 09:35:29
 */
public class SignUtil {
    private static final String SALT = "Aoh_1DFPiMzTNm*3S9TwLeZfEA$yhY5!!hPez@zbPihIul_8321!URTscBYYXIZcHoXSvFTBb!Mb0IsXBw!XkNpLKE%MFGfMZysj%qFhPIaDM0i9Y@^*Oyn64GKF@Wq0\n" +
            "0No$GVmJ7UPqfzGN5nA?_fr6u8StpqcP!ZEecgRZ.H?e0$NBM0zP8T!5X2eih^82xyNAha6IyqH0uxiDXU$f!wRKXH@9qsMx3Vxgykp4EzVFrqJqfcHVsN00RHnIdh4_\n" +
            "J_^!Qls.T6pIe21CpO4Dj1vR!d!o9VlHHpgX.HckMaXYX2D6DA3Z$U3gVGrZfAQSPBvLu!2f?wr1NNRNV!hH7rD.hovD3JWcp!QRg$VFLzz0?liQDbL54C.@K^7AHC7_\n" +
            "FRlPc?D5U7bD^txPi4RH1jLTZRn2WYj_K4NufOge1.?7hk4yWYpDoYQjoa!Q5iIEi943_L1juJ%GGhxPNhCC*Ow1extjBTo^HhZKbXI^mu0T?R92T3W4$*KZR$9cK^!t\n" +
            "Nj@o0p^SuJt1rJmaxbv5RA1BZX*KGgw^0pFoTw_mDNeujZ6.RKgnn0@?e4a7^IzlQ0QkwqZ7Ke.WOCubnP6hgsi2Il.Xe2cbjm*.!sqNW58IsGY3jQDUX__^bmgvvGIN\n" +
            "lWlC970t8ZV^VqQZ4gJ%Un^dd87KO6F4o2$BbFq8f*pFmBYP?nVQR*ya7DBjpuU*NRBTkcjX2@YO8@rp%vmI9N8uwRUwd5Z*W.z8bu*kPmljTI?Ugeu_ytZ918w2_yNG\n" +
            "?roQWr4oM3cWrQ4%QiUyd74yPLzd2NOgiiVLdzC$CGzvtZ7tHbMX@q1eqC3x!bzp%Q_fcOn?@F2nGt^tO4yXViZqMBIG@mpRgf@.v18Rq1MuZFBYf@WKRy21e.Wni5Dc\n" +
            "cZZ03U%ruEUyMR%9S$YZqcCx@PJW1ke9K.5rdT3m1DOIs3b7gsJ$QZ7Vyu511$otllaVO!_oHFLAxme9Jo3vVcUO92VwkqXoJ_boJVJZC2GHHxVr25hoxIu4i3kLiyt@\n" +
            "U2T8Jz2yD*SXHYyn.qlWhu3p7sL4vS8Oi9x6zrNN.zA_^9r2v%aMJgT1IJFsgnex@^9PVf2Sd!xh@G9e9wYLNEN*rJ5dulqrk*7jCczXfKO*.zibURxvbz$Mn2EVj0%K\n" +
            "BMk62SSdgX5q0OyfM71^1f8_fUzKJN6P27jPHSCDY0fE?^kX*0Yr2hW9WZjvVJHc4OoXj1V!B6aAtFDbc0SZB.qYxaWGFsgmI7*^pyg9bG6KfT!Y8_w.MJmk5SbCvL0X\n" +
            "1lURyqM$jt1cyE.wotgcKmcJPm4iK*kzA0uvz$K8uT*NXHh9!WFzs3yH7pfobTDl0YxwfccMElxFruxNO@pK!%O.T!4ul@D$Y.Q*?SuhDK7pswatK5$gxW0o_GvR4B%$\n" +
            "*Bs%T5bxdAcp5y^^D99V!udWeblcdS%cBvXXEqb$TOdfDFk2PIqF@O%C^kYDq6@_Uiu_6wk*pLwzZt.?ne!$ac*GdI.XGzUc8DrL1ANKH46JPrwC2b!pNVh*afCQmW*x\n" +
            "Okfidu7dE0iKj!K_igdNNw$M!$3jdq*BclpfF.RWmX5*9d!KU@*$48?OwuKYWdgER^V!EJ^hBkv72@.IJn@D4czH72kr*jnBzArR1lPqpY5wXYjQscES0r0S40aVe?2M\n" +
            "Q7g%k57V7jpZ7xSx$_wxa?FJ4pFHQqknc75g9Y2eE2q5OhJWq!900P7?zxIqHMdb*9j1fcTz^xjbsy!Gl%HwIivdLAFTqbCNLCsQU3_N0g2TRxP1_RGZ%_HX0it@muM0\n" +
            "^.gUQLCDR5iOIq%t9n1ijZbcOZIaDxrZc9qUB6GrJQ$WRi4GTS$rDm^HA9ghrCKwAOj2%1bdj4ue0Uj5k$aOhoxcIn^ZLUc7J2NiCnbBZ@eY3%S4IH3lgg7y6_8li5JX\n" +
            "tAbVPqyqw74s%RqiblZ04JAiBD1MPV7tiZ4a.5QrqrS%yNVgTmRlh2o$66z.xaKI6P9Gu*7ua*R%yX*XbGsR2Zyrf.as*qlCllbOPHif@*YiMA?_Nb_sXiU%TkFRFyW!";


    private static String generate(ClientRequest request) {
        ClientRequest.QryReq qryReq = request.getQryReq();
        String guid = qryReq.getGuid();
        String corpNo = qryReq.getCopNo();
        String corpLic = qryReq.getCopLic();
        String source = guid + corpNo + SALT + corpLic;
        return MD5.create().digestHex(source);
    }

    public static ClientRequest sign(ClientRequest request) {
        String signVl = generate(request);
        request.getQryReq().setSignInfo(signVl);
        return request;
    }

    public static boolean check(ClientRequest request) {
        String sigVl = generate(request);
        String s = request.getQryReq().getSignInfo();
        return s == null || s.equals(sigVl);
    }

    private static String generate(ClientResponse response) {
        ClientResponse.QryRetn qryRetn = response.getQryRetn();
        String guid = qryRetn.getGuid();
        ClientResponse.BizData bizData = response.getBizData();
        String data = JAXBUtils.obj2Xml(bizData);
        String source = guid + SALT + data;
        return MD5.create().digestHex(source);
    }

    public static ClientResponse sign(ClientResponse response) {
        String signVl = generate(response);
        response.getQryRetn().setSignInfo(signVl);
        return response;
    }

    public static boolean check(ClientResponse response) {
        String sigVl = generate(response);
        String s = response.getQryRetn().getSignInfo();
        return s == null || s.equals(sigVl);
    }

    private static String generate(ClientPutRequest request) {
        ClientPutRequest.ReqHead reqHead = request.getReqHead();
        String guid = reqHead.getGuid();
        String msgId = reqHead.getCopMsgId();
        String copLic = reqHead.getCopLic();
        ClientPutRequest.ReqData reqData = request.getReqData();
        String data = reqData.getData();
        String source = guid + msgId + SALT + copLic + data;
        return MD5.create().digestHex(source);
    }

    public static ClientPutRequest sign(ClientPutRequest request) {
        String signVl = generate(request);
        request.setSignature(signVl);
        return request;
    }

    public static boolean check(ClientPutRequest request) {
        String signVl = generate(request);
        String s = request.getSignature();
        return s == null || s.equals(signVl);
    }

    private static String generate(ClientGetRequest request) {
        ClientGetRequest.ReqHead reqHead = request.getReqHead();
        String guid = reqHead.getGuid();
        String copLic = reqHead.getCopLic();
        String source = guid + SALT + copLic;
        return MD5.create().digestHex(source);
    }

    public static ClientGetRequest sign(ClientGetRequest request) {
        String signVl = generate(request);
        request.setSignature(signVl);
        return request;
    }

    public static boolean check(ClientGetRequest request) {
        String signVl = generate(request);
        String s = request.getSignature();
        return s == null || s.equals(signVl);
    }
}
